# Copyright 2021 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =============================================================================

load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@build_bazel_rules_nodejs//:index.bzl", "pkg_npm")
load("//tools:copy_to_dist.bzl", "copy_to_dist", "copy_ts_library_to_dist")
load("//tools:tfjs_bundle.bzl", "tfjs_bundle")
load("//tools:tfjs_web_test.bzl", "tfjs_web_test")

package(default_visibility = ["//visibility:public"])

tfjs_bundle(
    name = "tf-backend-webgpu",
    entry_point = "//tfjs-backend-webgpu/src:index.ts",
    external = [
        "@tensorflow/tfjs-core",
        "@webgpu/types",
        "node-fetch",
        "util",
    ],
    globals = {
        "@tensorflow/tfjs-core": "tf",
    },
    umd_name = "tf",
    deps = [
        "//tfjs-backend-webgpu/src:tfjs-backend-webgpu_lib",
        "//tfjs-backend-webgpu/src:tfjs-backend-webgpu_src_lib",
    ],
)

copy_ts_library_to_dist(
    name = "copy_src_to_dist",
    srcs = [
        "//tfjs-backend-webgpu/src:tfjs-backend-webgpu_lib",
        "//tfjs-backend-webgpu/src:tfjs-backend-webgpu_src_lib",
    ],
    root = "src",
)

copy_to_dist(
    name = "copy_bundles",
    srcs = [
        ":tf-backend-webgpu",
        ":tf-backend-webgpu.es2017",
        ":tf-backend-webgpu.es2017.min",
        ":tf-backend-webgpu.fesm",
        ":tf-backend-webgpu.fesm.min",
        ":tf-backend-webgpu.min",
        ":tf-backend-webgpu.node",
    ],
)

copy_file(
    name = "copy_miniprogram",
    src = ":tf-backend-webgpu.min.js",
    out = "dist/miniprogram/index.js",
)

copy_file(
    name = "copy_miniprogram_map",
    src = ":tf-backend-webgpu.min.js.map",
    out = "dist/miniprogram/index.js.map",
)

pkg_npm(
    name = "tfjs-backend-webgpu_pkg",
    package_name = "@tensorflow/tfjs-backend-webgpu",
    srcs = [
        "README.md",
        "package.json",
    ],
    tags = ["ci"],
    deps = [
        ":copy_bundles",
        ":copy_miniprogram",
        ":copy_miniprogram_map",
        ":copy_src_to_dist",
    ],
)

STATIC_FILES = [
    # Listed here so sourcemaps are served
    "//tfjs-backend-webgpu/src:tfjs-backend-webgpu_test_bundle",
]

tfjs_web_test(
    name = "tfjs-backend-webgpu_test",
    srcs = [
        "//tfjs-backend-webgpu/src:tfjs-backend-webgpu_test_bundle",
    ],
    args = [
        "--testEnv",
        "webgpu",
        "--flags",
        '{"WEBGPU_CPU_FORWARD": false}',
    ],
    browsers = [
        "bs_chrome_mac_webgpu",
    ],
    local_browser = select({
        "@bazel_tools//src/conditions:linux_x86_64": "chrome_webgpu_linux",
        "@bazel_tools//src/conditions:windows": "chrome_webgpu",
        "//conditions:default": "chrome_webgpu",
    }),
    static_files = STATIC_FILES,
)
